import React from 'react';
import { Tree } from 'antd';
import styles from './style.less';

const DepartmentTree = ({ treeData = [], onSelect }) => {
    const renderTreeNode = data =>
        data.map(item => {
            if (item.children && item.children.length) {
                return (
                    <Tree.TreeNode key={item.key} title={item.title}>
                        {renderTreeNode(item.children)}
                    </Tree.TreeNode>
                );
            }
            return <Tree.TreeNode key={item.key} title={item.title} />;
        });

    return (
        <div className={styles.wrap}>
            <Tree defaultExpandAll onSelect={onSelect}>
                {treeData.map(item => (
                    <Tree.TreeNode key={item.key} title={item.title}>
                        {renderTreeNode(item.children)}
                    </Tree.TreeNode>
                ))}
            </Tree>
        </div>
    );
};

export default DepartmentTree;
